#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

struct BoxMullerState {
  double x1, x2, w, y1, y2;
  int useLast;
  struct drand48_data random;
};

void initBoxMullerState(struct BoxMullerState* state);
double boxMullerRandom(struct BoxMullerState* state);
double max(double a, double b);

int main() {
  struct BoxMullerState state;
  initBoxMullerState(&state);

  double rnd, S, E, r, sigma, T, t, sum = 0;
  int M, i;
  scanf(" %lf %lf %lf %lf %lf %d", &S, &E, &r, &sigma, &T, &M);
  double *trials = (double *) malloc( M * sizeof(double) );

  for (i=0; i<M; i++) {
    rnd = boxMullerRandom(&state);
    t = S*exp( (r - sigma*sigma/2.0)*T + (sigma*sqrt(T)*rnd) );    
    trials[i] = exp( -r*T)*max(t - E, 0.0);
    sum += trials[i];
  }

  double mean = sum/M;

  sum = 0;
  for (i=0; i<M; i++)
    sum += (trials[i] - mean)*(trials[i] - mean);

  double stddev = sqrt( sum/M );

  double confwidth = 1.96*stddev/sqrt(M);
  double confmin = mean - confwidth;
  double confmax = mean + confwidth;

  printf("%-9s%lf\n", "S", S);
  printf("%-9s%lf\n", "E", E);
  printf("%-9s%lf\n", "r", r);
  printf("%-9s%lf\n", "sigma", sigma);
  printf("%-9s%lf\n", "T", T);
  printf("%-9s%d\n", "M", M);
  printf("Confidence interval: (%lf, %lf)\n", confmin, confmax);

  free(trials);
  return 0;
}

void initBoxMullerState(struct BoxMullerState* state) {
  state->random.__init = 0;
  state->useLast = 0;

  struct timeval now;
  gettimeofday(&now, NULL);
  state->random.__x[0] = now.tv_usec;
}

double boxMullerRandom(struct BoxMullerState* state) {
  double randomNumber;

  if (state->useLast) {
    state->y1 = state->y2;
    state->useLast = 0;
  }
  else {
    do {
      drand48_r(&state->random, &state->x1);
      state->x1 = 2.0 * state->x1 - 1.0;
      drand48_r(&state->random, &state->x2);
      state->x2 = 2.0 * state->x2 - 1.0;
      state->w = state->x1 * state->x1 + state->x2 * state->x2;
    } while (state->w >= 1.0);

    state->w = sqrt((-2.0 * log(state->w)) / state->w);
    state->y1 = state->x1 * state->w;
    state->y2 = state->x2 * state->w;
    state->useLast = 1;
  }

  return state->y1;
}

double max(double a, double b) {
  return (a > b) ? a : b;
}

